package com.ruoyi.project.city.cityMasterList.controller;

import java.util.List;

import com.ruoyi.common.utils.PropertiesFileUtil;
import com.ruoyi.framework.web.service.ConfigService;
import com.ruoyi.project.system.config.domain.Config;
import com.ruoyi.project.system.config.service.IConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.constant.BusinessType;
import com.ruoyi.project.city.cityMasterList.domain.CityMasterList;
import com.ruoyi.project.city.cityMasterList.service.ICityMasterListService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 主城 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-08-28
 */
@Controller
@RequestMapping("/city/cityMasterList")
public class CityMasterListController extends BaseController
{

	Logger log = LoggerFactory.getLogger(this.getClass());

    private String prefix = "city/cityMasterList";
	
	@Autowired
	private ICityMasterListService cityMasterListService;
	@Autowired
	IConfigService configService;
	
	@RequiresPermissions("city:cityMasterList:view")
	@GetMapping()
	public String cityMasterList()
	{
	    return prefix + "/cityMasterList";
	}
	
	/**
	 * 查询主城列表
	 */
	@RequiresPermissions("city:cityMasterList:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(CityMasterList cityMasterList)
	{
		startPage();
        List<CityMasterList> list = cityMasterListService.selectCityMasterListListByJoin(cityMasterList);
		return getDataTable(list);
	}

	/**
	 * 新增主城
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存主城
	 */
	@RequiresPermissions("city:cityMasterList:add")
	@Log(title = "主城", action = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(CityMasterList cityMasterList)
	{		
		return toAjax(cityMasterListService.insertCityMasterList(cityMasterList));
	}

	/**
	 * 修改主城
	 */
	@GetMapping("/edit/{adCode}")
	public String edit(@PathVariable("adCode") Integer adCode, ModelMap mmap)
	{
		CityMasterList cityMasterList = cityMasterListService.selectCityMasterListById(adCode);
		mmap.put("cityMasterList", cityMasterList);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存主城
	 */
	@RequiresPermissions("city:cityMasterList:edit")
	@Log(title = "主城", action = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(CityMasterList cityMasterList)
	{		
		return toAjax(cityMasterListService.updateCityMasterList(cityMasterList));
	}
	
	/**
	 * 删除主城
	 */
	@RequiresPermissions("city:cityMasterList:remove")
	@Log(title = "主城", action = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(cityMasterListService.deleteCityMasterListByIds(ids));
	}



	/**
	 * 购买主城前查看数据
	 */
	@GetMapping("/beforeBuyCity/{adCode}")
	public String beforeBuyCity(@PathVariable("adCode") Integer adCode, ModelMap mmap)
	{
		CityMasterList cityMasterList = cityMasterListService.selectCityMasterListById(adCode);
		mmap.put("cityMasterList", cityMasterList);
		return prefix + "/buyCity";
	}

	/**
	 * 购买主城前
	 */
	@Log(title = "购买主城")
	@PostMapping("/buyCity")
	@ResponseBody
	public AjaxResult buyCity(@RequestParam String adCode, @RequestParam String userPhone)
	{
		log.info("【平台购买主城】用户信息:{},主城信息:{}", userPhone, adCode);

		//检查密码是否正确
//		String localPWD = PropertiesFileUtil.getInstance("CheckSignKey").get("BUY_CITY_PASSWD").toUpperCase();
//        if(!passWD.equals(localPWD)){
//
//        	log.error("【平台购买主城】输入密码错误:{}, 应该输入的密码:{}", passWD, localPWD);
//			return error("输入密码错误");
//		}

		AjaxResult result = cityMasterListService.buyCity(adCode, userPhone);

		return result;
	}


    /**
     * 后台操作人员开放城市操作
     * 城市开放后，客户端可以进行该城市的购买
     */
    @Log(title = "开放城市")
    @PostMapping("/openCity")
    @ResponseBody
    public AjaxResult openCity(@RequestParam String ids)
    {
        log.info("【开放主城】用户信息,主城信息:{}", ids);


        AjaxResult result = cityMasterListService.openCity(ids);

        return result;
    }

}
